//
//  CMDBPickerView.swift
//  CMDB
//
//  Created by 王磊 on 09/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

protocol CMDBPickerDelegate: NSObjectProtocol {
    
    func pickerConfirmTriggered(datePicker: CMDBPickerView)
}

class CMDBPickerView: CMDBBaseView {
    
    private var dataArray: Array<String> = []
    
    private var datePicker: UIPickerView = UIPickerView()
    
    private var cover: UIButton = UIButton.getInstance()
    
    private var bottomView: CMDBBaseView = CMDBBaseView.getInstance()
    
    private var confirmBtn: UIButton = UIButton.getInstance()
    
    private var cancleBtn: UIButton = UIButton.getInstance()
    
    internal var dateString: String?
    
    internal var delegate: CMDBPickerDelegate?
    
    internal var id: String = ""
    
    override func addOwnSubviews() {
        super.addOwnSubviews()
        
        addSubview(cover)
        
        addSubview(bottomView)
        
        bottomView.addSubview(datePicker)
        
        bottomView.addSubview(cancleBtn)
        
        bottomView.addSubview(confirmBtn)
        
        datePicker.frame = CGRectMake(0, 60 * CMDB_Rate, CMDB_Screen_Width, bottomView_height - 60 * CMDB_Rate)
        
        confirmBtn.snp_makeConstraints { (make) in
            
            make.right.equalTo(-20 * CMDB_Rate)
            make.width.equalTo(100 * CMDB_Rate)
            make.height.equalTo(50 * CMDB_Rate)
            make.top.equalTo(10 * CMDB_Rate)
        }
        cancleBtn.snp_makeConstraints { (make) in
            
            make.left.equalTo(20 * CMDB_Rate)
            make.width.equalTo(100 * CMDB_Rate)
            make.height.equalTo(50 * CMDB_Rate)
            make.top.equalTo(10 * CMDB_Rate)
        }
    }
    override func configOwnSubviews() {
        super.configOwnSubviews()
        
        cover.backgroundColor = UIColor.blackColor()
        
        cover.alpha = 0
        
        datePicker.delegate = self
        
        datePicker.dataSource = self
        
        bottomView.backgroundColor = UIColor.whiteColor()
        
        confirmBtn.setTitle("确认", forState: .Normal)
        
        confirmBtn.setTitle("确认", forState: .Highlighted)
        
        confirmBtn.setTitleColor(word87Color, forState: .Normal)
        
        confirmBtn.setTitleColor(word87Color, forState: .Highlighted)
        
        confirmBtn.titleLabel?.font = CMDB_17_FONT
        
        cancleBtn.setTitle("取消", forState: .Normal)
        
        cancleBtn.setTitle("取消", forState: .Highlighted)
        
        cancleBtn.setTitleColor(UIColor.blueColor(), forState: .Normal)
        
        cancleBtn.setTitleColor(UIColor.blueColor(), forState: .Highlighted)
        
        cancleBtn.titleLabel?.font = CMDB_17_FONT
        
        cover.addTarget(self, action: #selector(coverTriggered), forControlEvents: .TouchUpInside)
        
        cancleBtn.addTarget(self, action: #selector(coverTriggered), forControlEvents: .TouchUpInside)
        
        confirmBtn.addTarget(self, action: #selector(confirm), forControlEvents: .TouchUpInside)
    }
    
    internal var currentDate: String?
    
}

extension CMDBPickerView {
    
    
}
extension CMDBPickerView: UIPickerViewDataSource {}

extension CMDBPickerView {
    
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        
        return 1
    }
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        
        return dataArray.count
    }
}

extension CMDBPickerView: UIPickerViewDelegate {}

extension CMDBPickerView {
    
    func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        
        return 60 * CMDB_Rate
    }
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        
        return dataArray[row]
    }
    
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        
        currentDate = dataArray[row]
        
    }
}
private let pickerView_height: CGFloat = 500 * CMDB_Rate


extension CMDBPickerView {
    
}
extension CMDBPickerView {
    
    @objc private func coverTriggered() {
        
        dismiss(true)
    }
    
    @objc private func confirm() {
        
        dismiss(true)
        
        guard let delegate = delegate else {
            
            return
        }
        
        delegate.pickerConfirmTriggered(self)
    }
}

private let bottomView_height: CGFloat = 500 * CMDB_Rate

extension CMDBPickerView {
    
    internal func updateData(data: Array<String>) {
        
        if !dataArray.isEmpty {
            
            dataArray.removeAll()
        }
        
        dataArray += data
        
        datePicker.reloadAllComponents()
    }
    
    internal func show(animated: Bool) {
        
        let window = UIApplication.sharedApplication().keyWindow
        
        frame = CMDB_Screen_Bounds
        
        window?.addSubview(self)
        
        cover.frame = bounds
        
        bottomView.addSubview(datePicker)
        
        bottomView.frame = CGRectMake(0, CMDB_Screen_Height, CMDB_Screen_Width, bottomView_height)
        
        datePicker.selectRow(dataArray.count - 1, inComponent: 0, animated: false)
        
        currentDate = dataArray.last
        
        if animated {
            
            UIView.animateWithDuration(0.3, animations: {[weak self] in
                
                self!.cover.alpha = 0.5
                
                self!.bottomView.frame = CGRectMake(0, CMDB_Screen_Height - bottomView_height, CMDB_Screen_Width , bottomView_height)
                
                }, completion: { (flag) in
                    
            })
        }
    }
    internal func dismiss(animated: Bool) {
        
        if animated {
            UIView.animateWithDuration(0.3, animations: {[weak self] in
                self!.cover.alpha = 0
                
                self!.bottomView.frame = CGRectMake(0, CMDB_Screen_Height, CMDB_Screen_Width, bottomView_height)
                
                }, completion: { [weak self](flag) in
                    if flag {
                        
                        self!.removeFromSuperview()
                        
                    }
                })
        }
    }
}